<?php

namespace app\admin\controller\wx;

use app\admin\model\wx\WxMediaInfo;
use app\common\controller\AdminController;
use app\Request;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\View;

/**
 * @ControllerAnnotation(title="wx_reply")
 */
class WxReply extends AdminController
{
    protected $materialType = ['news', 'video', 'voice', 'image', 'music'];

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\wx\WxReply();

    }

    /**
     * Author:YangXinPeng
     * 关键字回复列表
     * @param Request $request
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function reply(Request $request)
    {
        $type = $request->get('type', 'default');
        $typeList = [
            [
                'url' => url('reply', ['type' => 'default']),
                'item' => "默认回复",
                "type" => 'default'
            ],
            ['url' => url('reply', ['type' => 'subscribe']),
                'item' => "关注回复",
                "type" => 'subscribe'
            ],
            ['url' => url('reply', ['type' => 'keyword']),
                'item' => "关键字回复",
                "type" => 'keyword'
            ],
        ];
        if ($type == 'default') {
            $info = \app\admin\model\wx\WxReply::where('type', $type)->select();
        } elseif ($type == 'subscribe') {
            $info = \app\admin\model\wx\WxReply::where('type', $type)->select();
        } elseif ($type == 'keyword') {
            $info = \app\admin\model\wx\WxReply::where('type', $type)->select();
        }
        $view = [
            'title' => '粉丝',
            'type' => $type,
            'info' => $info,
            'typeList' => $typeList,
        ];
        View::assign($view);
        View::engine()->layout(false);
        return View::fetch();
    }


    /**
     * Author:YangXinPeng
     * 关键字回复添加
     * @param Request $request
     * @return string|\think\response\Json
     */
    public function replyAdd(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            if (!$data['type']) {
                $data['type'] = 'keyword';
            }
            if ($data['data']) {
                $data['msg_type'] = 'text';
            }
            $res = \app\admin\model\wx\WxReply::create($data);
            if ($res) {
                return json(['code' => 200, 'msg' => '添加成功']);
            } else {
                return json(['code' => 400, 'msg' => '添加失败']);
            }
        }
        $view = [
            'info' => [],
            'title' => 'add',
            'materialGroup' => $this->getMaterialGroup(),
        ];
        View::assign($view);
        View::engine()->layout(false);
        return View::fetch();

    }

    /**
     * Author:YangXinPeng
     * 得到媒体分组
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getMaterialGroup()
    {
        $materialGroup = [];
        foreach ($this->materialType as $k => $v) {
            if ($v == 'news') {
                $weixin_material_info = \app\admin\model\wx\WxMedia::where('type', 'news')->select()->toArray();
                if (!empty($weixin_material_info)) {
                    foreach ($weixin_material_info as $key => $value) {
                        $item_info = WxMediaInfo::where('material_id', $value['id'])->select()->toArray();
                        $weixin_material_info[$key]['item_info'] = $item_info;
                    }
                }
                $materialGroup[$v] = $weixin_material_info;
            } else {
                $weixin_material_info = \app\admin\model\wx\WxMedia::where('type', $v)->select()->toArray();
                $materialGroup[$v] = $weixin_material_info;
            }
        }
        return $materialGroup;
    }


}